package com.mf.morefun.ui.userinfo;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.apkfuns.logutils.LogUtils;
import com.mf.morefun.R;
import com.mf.morefun.base.BaseBusActivity;
import com.mf.morefun.bean.PasswordBean;
import com.mf.morefun.constant.Configuration;
import com.mf.morefun.requestbean.RequestPasswordBean;
import com.mf.morefun.ui.dohomework.HomeworkListActivity;
import com.mf.morefun.widget.NetErrorPop;
import com.mf.morefun.widget.RoundProcessDialog;
import com.zhcx.zhcxlibrary.utils.AppManager;
import com.zhcx.zhcxlibrary.utils.FileUtils;
import com.zhcx.zhcxlibrary.utils.StringUtils;
import com.zhcx.zhcxlibrary.utils.ToastUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ThirdPasswordActivity extends BaseBusActivity {
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.iv_show)
    ImageView ivShow;
    @BindView(R.id.tv_next)
    TextView tvNext;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    private RoundProcessDialog dialog;

    @Override
    protected int getNaviteColor() {
        return 0;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_thirdpassword;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        dialog = new RoundProcessDialog(ThirdPasswordActivity.this, "");
        tvPhone.setText("请为您的账号" + getIntent().getStringExtra("phone") + "设置一个新密码");
    }

    @Override
    protected void initData() {

    }
    int count = 0;
    @OnClick({R.id.iv_show, R.id.tv_next, R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_show:
                if ((count % 2) == 0) {
                    ivShow.setImageResource(R.mipmap.eye_show);
                    editPhone.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    ivShow.setImageResource(R.mipmap.not_show_password);
                    editPhone.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                count++;
                break;
            case R.id.tv_next:
                //
                if (StringUtils.isEmpty(editPhone.getText().toString())){
                    ToastUtils.show(getApplicationContext(), "请输入密码", 100);
                    return;
                }
                if (editPhone.getText().toString().length() < 6 || editPhone.getText().toString().length() > 10){
                    ToastUtils.show(getApplicationContext(), "密码为6-10位数字或字母", 100);
                    return;
                }
                if (!StringUtils.isSurePassword(editPhone.getText().toString())){
                    ToastUtils.show(getApplicationContext(), "密码为6-10位数字或字母", 100);
                    return;
                }
                //忘记秘密吗接口
                initPasswordHttp(editPhone.getText().toString(), getIntent().getStringExtra("phone"));
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }

    private void initPasswordHttp(String password, String phone) {
        if (null != dialog){
            dialog.show();
        }
        RequestParams params = new RequestParams(Configuration.HOST + Configuration.CHANGEPASSWORD);
        RequestPasswordBean requestPasswordBean = new RequestPasswordBean();
        requestPasswordBean.setNewPassword(FileUtils.md5(password));
        requestPasswordBean.setPhone(phone);
        params.setBodyContent(JSON.toJSONString(requestPasswordBean));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (null != dialog){
                    dialog.cancel();
                }
                LogUtils.d(result);
                if (!StringUtils.isEmpty(result)){
                    PasswordBean passwordBean = JSON.parseObject(result, PasswordBean.class);
                    if (passwordBean.getErrorCode() == 0){
                        AppManager.getAppManager().finishAllActivity();
                        Intent mIntent = new Intent(ThirdPasswordActivity.this, LoginActivity.class);
                        startActivity(mIntent);
                        finish();
                    }
                    ToastUtils.show(getApplicationContext(), passwordBean.getMessage(), 100);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                if (null != dialog){
                    dialog.cancel();
                }
                mHandler.sendEmptyMessage(101);

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 101:
                    NetErrorPop errorPop = new NetErrorPop(ThirdPasswordActivity.this);
                    errorPop.showPop("");
                    break;
            }
        }
    };

}
